<?php

//UMA new
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class InputItem extends Model
{
    const HIDDEN_ITEMS = [
        'driver_lesson_number',
        'working_school_name',
        'working_school_name_kana',
        'working_school_zip',
        'working_school_pref',
        'working_school_city',
        'working_school_town',
        'working_school_building',
        'working_school_tel',
        'work_department',
        'employment_start_date',
        'yearly_income',
        'rent',
        'balance',
        'monthly_autoloan_repayment',
        'monthly_cashloan_repayment',
        'monthly_otherloan_repayment',
        'age',
        'year_of_birth',
        'working_years',
    ];

    protected $fillable = [
        'input_group_id',
        'input_item',
        'identifier',
        'input_type',
    ];

    /**
     * 每个 item 属于一个group
     * すべてのアイテムはグループに属します
     * @return belongsTo
     */
    public function group()
    {
        return $this->belongsTo(InputGroup::class, 'input_group_id');
    }

    /**
     * 一个item有多个options
     * 1つのアイテムには多くのオプションがあります
     * @hasMany
     */
    public function options()
    {
        return $this->hasMany(InputOption::class, 'input_item_id');
    }
    
    /**
     * scopeByIdentity
     *
     * @param  mixed $query
     * @param  mixed $identity
     * @return query
     */
    public function scopeByIdentity($query, $identity)
    {
        return $query->where('identifier', $identity);
    }
    
    
    public function getOptionByValue($value) {

        if($this->options->isNotEmpty()) {
           
            return  $this->options->sortByDesc('option')->first(function ($item) use ($value) {
                if($this->isTextInput()) {
                    $calculatedValue = $this->calculateValue($value);
                    $item->value = $this->isDate() ? $value : $calculatedValue;
                    return $item->option < ($calculatedValue + 1);
                }
                return $item->option == $value;
            });
        }
        return $this->options()->make(['option_name' => $value, 'rate' => 1]);
    }

    
    /**
     * isTextInput
     *
     * @return boolean
     */
    public function isTextInput()
    {
        $textFields = [
            'year_of_birth',
            'yearly_income',
            'balance',
            'employment_start_date'
        ];
        return $this->options->isEmpty() || in_array($this->identifier, $textFields);
    }
    
    /**
     * get_year_of_birth_value
     *
     * @param  mixed $value
     * @return String
     */
    public function get_year_of_birth_value($value)
    {
        return  now()->year - $value;
    }


    public function get_employment_start_date_value($value) {
        return now()->year - Carbon::parse($value)->year;
    }
    
    
    /**
     * get_method
     *
     * @return String
     */
    public function get_method()
    {
        return 'get_'.$this->identifier.'_value';
    }

    public function isDate() {
        $dateFields = [
            'year_of_birth',
            'employment_start_date',
        ];
        return in_array($this->identifier, $dateFields);
    }

    public function calculateValue($value) {
        $method = $this->get_method();
        if(method_exists($this, $method)) {
            return $this->$method($value);
        }
        return $value;
    }
}
